import Vue from 'vue'
// import {userNoticeList} from '@/api/my.js'
import {
	genTestUserSig
} from '@/utils/debug/GenerateTestUserSig.js'
import TencentCloudChat from '@tencentcloud/chat';
import {
	TUIChatKit,
} from "@/TUIKit";
uni.TUIChatKit=TUIChatKit
// #ifdef H5
window.chat = TUIChatKit
// #endif
import TimChat from "@/utils/tim.js"

import {
	TUILogin
} from "@tencentcloud/tui-core";
export default {
	namespaced: true,
	state() {
		return {
			userInfo: {},
			count: 0,
			msglist: [],
			noticelist: [],
			timLogin: false,
		}
	},
	mutations: {
		set_data(state, params) {
			state[params.type] = params.params
		},
		async chat(state, ID) {
			const Id= (ID+"").includes("C2C")?Id:'C2C' + ID
			console.log("聊天:"+Id)
		
			await TUIChatKit.TUIChatEngine.TUIConversation.switchConversation(Id).catch(e =>
				console.log(e))
			uni.navigateTo({
				url: "/TUIKit/components/TUIChat/index",
			});
		},
			
		async sendMsg(state,{
			userID,Text,Type
		}){
			
			let message = uni.TUIChatKit.chat.createCustomMessage({
				to: userID+"",
				conversationType: TencentCloudChat.TYPES.CONV_C2C,
				payload: {
					data: Text,
					extension: Type||"text",
				}
			});
			await uni.TUIChatKit.chat.sendMessage(message);
		},
		setData(state, params) {
			console.log("设置" + params.key)
			state[params.key] = params.value
		},
		getcount(state, params) {
			// userNoticeList(params).then(res=>{
			// 	// console.log(res)
			// 	state.count=res.data.data.count
			// 	uni.setTabBarBadge({
			// 		index:3,
			// 		text:String(res.data.data.count)
			// 	})
			// 	state.msglist=res.data.data.consult_list
			// 	state.noticelist=res.data.data.notice_list

			// })
		}
	},
	actions: {
		timLogin({
			state
		}) {
			return new Promise(async (reslove, reject) => {
				state.timLogin = false
				const userID = state.userInfo ? state.userInfo.user_id + '' : '';
				console.log("登录IM"+userID)
				if (userID) {
					const config = {
						userID: userID+ '',
						SDKAppID: 1600090539,
						secretKey: "8e95af363bc1a1471ed52b93a3d45fd87f0af5d086cb9ff77c2edad3dd27e3aa",
						useUploadPlugin: true,
						useProfanityFilterPlugin: false,
						framework: `vue2`
					};
					config.userSig = genTestUserSig(config).userSig
					TUIChatKit.init();
					
					await TUILogin.login(config);
					new TimChat()
					TUIChatKit.chat.updateMyProfile({
						nick: state.userInfo.username,
						avatar: state.userInfo.avatar,
					}).catch(e => console.log(e))
					// uni.$tim.login({
					// 	userID,
					// 	userSig: config.userSig
					// }).then(imResponse => {
					// 	console.log(imResponse);
					// 	console.log("登录状态")
					// 	if (imResponse.data.errorCode === 0) {
					// 		console.log("登录状态成功")
					// 		state.timLogin = true
					// 		reslove()
					// 	} else {
					// 		console.log("登录状态失败")
					// 		reject()
					// 	}
					// }).catch(res => {
					// 	console.log(res)
					// })
				}
				reslove()
			})
		},
		timLogout({
			state
		}) {
			return new Promise(async (reslove, reject) => {
				state.timLogin = false
				const userID = state.userInfo ? state.userInfo.user_id + '' : '';
				console.log("登录IM"+userID)
				if (userID) {
					const config = {
						userID: userID+ '',
						SDKAppID: 1600069596,
						secretKey: "0e0573d254c832da06bb30c9e0da20cc13bf45f3dc7796bfa9b9caf74d6f445b",
						useUploadPlugin: true,
						useProfanityFilterPlugin: false,
						framework: `vue2`
					};
					config.userSig = genTestUserSig(config).userSig
					TUIChatKit.init();
					
					await TUILogin.logout(config);
					
					
					// uni.$tim.login({
					// 	userID,
					// 	userSig: config.userSig
					// }).then(imResponse => {
					// 	console.log(imResponse);
					// 	console.log("登录状态")
					// 	if (imResponse.data.errorCode === 0) {
					// 		console.log("登录状态成功")
					// 		state.timLogin = true
					// 		reslove()
					// 	} else {
					// 		console.log("登录状态失败")
					// 		reject()
					// 	}
					// }).catch(res => {
					// 	console.log(res)
					// })
				}
				reslove()
			})
		},
	}
};